from rest_framework.generics import ListAPIView, RetrieveAPIView
from .models import CourseCategory, Course, CourseChapter
from .serializers import CourseCategoryModelSerializer, CourseModelSerializer, CourseRetrieveSerializer, \
    CourseChapterSerializer
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from .paginations import CustomPageNumberPagination


# 课程分类
class CourseCategoryListApiView(ListAPIView):
    queryset = CourseCategory.objects.filter(is_show=True, is_deleted=False).order_by('orders')
    serializer_class = CourseCategoryModelSerializer


# 课程查询
class CourseListAPIView(ListAPIView):
    queryset = Course.objects.filter(is_deleted=False, is_show=True).order_by("orders")
    serializer_class = CourseModelSerializer
    # 设置过滤器和排序后端
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    # 导入自定义分页器
    pagination_class = CustomPageNumberPagination
    # 设置允许过滤的字段: 按照课程分类过滤
    filterset_fields = ('course_category',)

    # 允许排序的字段：按照id\学生人数\价格排序
    ordering_fields = ('id', 'students', 'price')


class CourseRetrieveAPIView(RetrieveAPIView):
    queryset = Course.objects.filter(is_deleted=False, is_show=True)
    serializer_class = CourseRetrieveSerializer


class CourseChapterListApiView(ListAPIView):
    queryset = CourseChapter.objects.filter(is_deleted=False, is_show=True).order_by('chapter')
    serializer_class = CourseChapterSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ['course', ]
